using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using GonzalesCore.CoreElements;
using GonzalesCore.CoreManagers;
using Kypris2.CoreElements;
using Kypris2.Utils;
using Jamila2.Tools;

public partial class GonzalesAdminErrorLog : System.Web.UI.Page
{
    protected PrivateLoginSession CurrentPrivateSession = null;
    protected LogManager CurLogManager = null;

    #region CurrentPageIndex
    protected int CurrentPageIndex
    {
        get
        {
            return (int)ViewState["CurrentPageIndex"];
        }

        set
        {
            ViewState["CurrentPageIndex"] = value;
        }
    }
    #endregion

    #region ResultTable
    protected DataTable ResultTable
    {
        get
        {
            return (DataTable)ViewState["ResultTable"];
        }

        set
        {
            ViewState["ResultTable"] = value;
        }
    }
    #endregion

    #region GridViewHeader
    protected ArrayList GridViewHeader
    {
        get
        {
            return (ArrayList)ViewState["GridViewHeader"];
        }

        set
        {
            ViewState["GridViewHeader"] = value;
        }
    }
    #endregion

    #region Events

    #region Page_Load
    protected void Page_Load(object sender, EventArgs e)
    {
        this.SessionCheck();

        this.Table_MessageBox.Visible = false;

        if (!IsPostBack)
        {
            this.CurrentPageIndex = 0;
            this.PageComponentSetup();
            this.SwitchLanguage();

            this.LoadData();

            if (this.ResultTable != null)
            {
                DataView ResultView = new DataView(this.ResultTable);
                ResultView.Sort = this.DropDownList_SortBy.SelectedValue + " " + this.DropDownList_SortOrder.SelectedValue;
                this.BindData(ResultView, this.CurrentPageIndex);

            }
        }
    }
    #endregion

    #region DropDownList_SortBy_SelectedIndexChanged
    protected void DropDownList_SortBy_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataView ResultView = new DataView(this.ResultTable);
        ResultView.Sort = this.DropDownList_SortBy.SelectedValue + " " + this.DropDownList_SortOrder.SelectedValue;
        this.BindData(ResultView, this.CurrentPageIndex);
    }
    #endregion

    #region DropDownList_SortOrder_SelectedIndexChanged
    protected void DropDownList_SortOrder_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.DropDownList_SortBy_SelectedIndexChanged(sender, e);
    }
    #endregion

    #region GridViewResult_PageIndexChanging
    protected void GridViewResult_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.CurrentPageIndex = e.NewPageIndex;
        this.DropDownList_SortBy_SelectedIndexChanged(sender, e);
    }
    #endregion

    #region Button_Search_Click
    protected void Button_Search_Click(object sender, EventArgs e)
    {
        if (this.ResultTable != null)
        {
            DataView ResultView = new DataView(this.ResultTable);
            ResultView.RowFilter = "";

            if (this.TextBox_ErrorID.Text.Length > 0 && this.TextBox_ErrorID.Text.CompareTo("[Error ID]") != 0)
            {
                ResultView.RowFilter = "ID = '" + this.TextBox_ErrorID.Text + "'";
            }

            if (this.TextBox_DateFrom.Text.Length > 0 && this.TextBox_DateTo.Text.Length > 0)
            {
                ResultView.RowFilter += (ResultView.RowFilter.Length > 0) ? " AND LogDateTime >= '" + SwissArmy.DateConvert(this.TextBox_DateFrom.Text, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedDateFormat).ToShortDateString() + "' AND LogDateTime <= '" + SwissArmy.DateConvert(this.TextBox_DateTo.Text, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedDateFormat).ToShortDateString() + "'" : " LogDateTime >= '" + SwissArmy.DateConvert(this.TextBox_DateFrom.Text, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedDateFormat).ToShortDateString() + "' AND LogDateTime <= '" + SwissArmy.DateConvert(this.TextBox_DateTo.Text, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedDateFormat).ToShortDateString() + "'";
            }

            this.CurrentPageIndex = 0;
            this.BindData(ResultView, this.CurrentPageIndex);
        }
    }
    #endregion

    #region Button_ClearLog_Click
    protected void Button_ClearLog_Click(object sender, EventArgs e)
    {
        if (this.CurLogManager.ClearErrorLog())
        {
            // log down the history
            this.CurLogManager.LogMessage("[GonzalesAdminErrorLog] : Error Log Cleared", this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.User);

            this.CurrentPageIndex = 0;
            this.LoadData();
            DataView ResultView = new DataView(this.ResultTable);
            this.BindData(ResultView, this.CurrentPageIndex);
        }
    }
    #endregion

    #region OnRowCreated
    protected void OnRowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            TableCellCollection cells = e.Row.Cells;

            for (int i = 0; i < this.GridViewHeader.Count; i++)
            {
                cells[i].Text = this.GridViewHeader[i].ToString();
            }
        }
    }
    #endregion

    #endregion

    #region Customized Methods

    #region SessionCheck
    public void SessionCheck()
    {

        if (Session[GonzalesCommonNamingConvention.CurrentPrivateSession] != null)
        {
            this.CurrentPrivateSession = (PrivateLoginSession)Session[GonzalesCommonNamingConvention.CurrentPrivateSession];
            this.CurLogManager = new LogManager(this.CurrentPrivateSession);    // activate Log Manager
        }
        else
        {
            Response.Redirect(GonzalesErrorPageRedirect.SessionError);
        }

    }
    #endregion

    #region PageComponentSetup
    protected void PageComponentSetup()
    {



        this.TextBox_DateTo.Attributes["onclick"] = this.TextBox_DateFrom.Attributes["onclick"] = "popUpCalendar(this, this, '" + SwissArmy.DateFormatForDisplay(this.CurrentPrivateSession.DateFormatInUse, "/") + "');"; 
        
    }
    #endregion

    #region SwitchLanguage
    protected void SwitchLanguage()
    {
        try
        {
            using (MultiLingualFactory CurLanguageFactory = MultiLingualEngine.GetMultiLingualFactory(this, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage.InternalID))
            {
                this.Label_Information.Text = CurLanguageFactory.DisplayValue("Information");
                this.Label_PageTitle.Text = CurLanguageFactory.DisplayValue("PageTitle");
                this.Label_To.Text = CurLanguageFactory.DisplayValue("To");
                this.Button_ClearLog.Text = CurLanguageFactory.DisplayValue("ClearLog");
                this.Button_Search.Text = CurLanguageFactory.DisplayValue("Search");

                this.GridViewHeader = new ArrayList();
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("ID"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("LogDateTimeInString"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("Claimant"));
                this.GridViewHeader.Add(CurLanguageFactory.DisplayValue("Description"));

                this.DropDownList_SortBy.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("LogDateTimeInString"), "LogDateTime"));
                this.DropDownList_SortBy.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("Claimant"), "Claimant"));

                this.DropDownList_SortOrder.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("ASC"), "ASC"));
                this.DropDownList_SortOrder.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("DESC"), "DESC"));
                this.DropDownList_SortOrder.SelectedIndex = 1;

                this.Button_ClearLog.Attributes["onclick"] = "return confirm('" + CurLanguageFactory.DisplayValue("ConfirmedMessage") + "');";

            }
        }
        catch (Exception ex)
        {
            this.CurLogManager.LogError("[GonzalesAdminAuthorizedDetail.SwitchLanguage.EX] : " + ex.Message, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.User);
        }

    }
    #endregion

    #region ShowErrorMessage
    protected void ShowErrorMessage(string ErrorMessage)
    {
        this.Table_MessageBox.Visible = true;
        this.Table_MessageBox.Rows[0].Cells[0].Text = ErrorMessage;
        this.Table_MessageBox.Rows[0].Cells[0].CssClass = "ErrorBox";
    }
    #endregion

    #region ShowMessage
    protected void ShowMessage(string Message)
    {
        this.Table_MessageBox.Visible = true;
        this.Table_MessageBox.Rows[0].Cells[0].Text = Message;
        this.Table_MessageBox.Rows[0].Cells[0].CssClass = "MessageBox";
    }
    #endregion

    #endregion

    #region Data Operation

    protected void LoadData()
    {
        LogsCollection ErrorCollection = this.CurLogManager.GetAllErrorLog();
        if (ErrorCollection != null)
        {
            this.ResultTable = new DataTable();

            ResultTable.Columns.Add(new DataColumn("ID", Type.GetType("System.String")));
            ResultTable.Columns.Add(new DataColumn("LogDateTime", Type.GetType("System.DateTime")));
            ResultTable.Columns.Add(new DataColumn("LogDateTimeInString", Type.GetType("System.String")));
            ResultTable.Columns.Add(new DataColumn("Claimant", Type.GetType("System.String")));
            ResultTable.Columns.Add(new DataColumn("Description", Type.GetType("System.String")));

            for (int x = 0; x < ErrorCollection.Count; x++)
            {
                ErrorLogItem CurLogItem = (ErrorLogItem)ErrorCollection[x];

                DataRow ResultRow = this.ResultTable.NewRow();

                ResultRow["ID"] = CurLogItem.LogID;
                ResultRow["LogDateTime"] = CurLogItem.LogDate;
                ResultRow["LogDateTimeInString"] = SwissArmy.DateConvert(CurLogItem.LogDate, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedDateFormat, "/", true);
                ResultRow["Claimant"] = (CurLogItem.LoggedBy.InternalID.Length == 36) ? CurLogItem.LoggedBy.DisplayName + " (" + CurLogItem.LoggedBy.EmailID + ")" : "";
                ResultRow["Description"] = CurLogItem.Description;

                this.ResultTable.Rows.Add(ResultRow);

            }

        }

    }

    private void BindData(DataView result)
    {
        this.GridViewResult.DataSource = result;
        this.GridViewResult.DataBind();
    }

    private void BindData(DataView result, int pageindex)
    {
        this.GridViewResult.PageIndex = pageindex;
        this.GridViewResult.DataSource = result;
        this.GridViewResult.DataBind();
    }

    #endregion


}
